package cmd

import (
	"context"

	"gitee.com/zawei/yggdrasil/third_party/urfave/cli"
)

type Option func(o *Options)

type Options struct {
	// For the Commands Line itself
	Name        string
	Description string
	Version     string
	Flags       []cli.Flag
	Commands    []*cli.Command
	Action      cli.ActionFunc

	// Other options for implementations of the interface
	// can be stored in a context
	Context context.Context
}

// Commands line Name
func Name(n string) Option {
	return func(o *Options) {
		o.Name = n
	}
}

// Commands line Action
func Action(f cli.ActionFunc) Option {
	return func(o *Options) {
		o.Action = f
	}
}

// Commands line Description
func Description(d string) Option {
	return func(o *Options) {
		o.Description = d
	}
}

// Commands line Version
func Version(v string) Option {
	return func(o *Options) {
		o.Version = v
	}
}

// Commands line flag
func Flags(flags ...cli.Flag) Option {
	return func(o *Options) {
		o.Flags = append(o.Flags, flags...)
	}
}

// Commands line Commands
func Command(cmds ...*cli.Command) Option {
	return func(o *Options) {
		o.Commands = append(o.Commands, cmds...)
	}
}

// Commands line sub Commands
func SubCommand(name string, cmds ...*cli.Command) Option {
	return func(o *Options) {
		for _, cmd := range o.Commands {
			if cmd.Name == name {
				cmd.Subcommands = append(cmd.Subcommands, cmds...)
			}
		}
	}
}
